//
//  HomeViewController.m
//  CherryPlumStore
//
//  Created by SDX on 2019/9/4.
//  Copyright © 2019 千凯. All rights reserved.
//

#import "HomeViewController.h"
#import "CombinationViewController.h"
#import "HomeLoader.h"
@interface HomeViewController ()<UIBarPositioningDelegate>

@property(nonatomic, copy, readwrite) UIScrollView *scrollView;

@end

@implementation HomeViewController
-(instancetype) init {
    self = [super init];
    if (self) {
        self.tabBarItem.title = @"首页";
        self.tabBarItem.image = [[UIImage imageNamed:@"tabbar_home_n"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        self.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_home_s"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [self.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:51.0/255 green:51.0/255 blue:51.0/255 alpha:1]} forState:UIControlStateNormal];
        [self.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:255.0/255 green:99.0/255 blue:6.0/255 alpha:1]} forState:UIControlStateSelected];
        
        _scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
        
        // 顶部背景弧线图
        UIImageView *homeTopBg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 44, SCREENWIDTH, 195)];
        
        homeTopBg.image = [UIImage imageNamed:@"home_top_bg"];

        [_scrollView addSubview:homeTopBg];
        _scrollView.backgroundColor = [UIColor redColor];
        [self.view addSubview:_scrollView];
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    [self addCustomNav];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadHome];
}

/**
 添加本页面的nav
 */
-(void) addCustomNav {
    UINavigationBar *nav = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, STATUSBARHEIGHT, SCREENWIDTH, 44)];
    UINavigationItem *navItem = [[UINavigationItem alloc]initWithTitle:@"樱桃李"];
    // 这里报警告的原因是 self是属于vc的; 自定义分类使用协议时出现与父类协议的冲突
    nav.delegate = (id)self;
    // 设置导航栏的背景图
    UIImage *navBgimage = [[UIImage imageNamed:@"home_nav_bg"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0) resizingMode:UIImageResizingModeStretch];
    [nav setBackgroundImage: navBgimage
             forBarPosition:UIBarPositionTop barMetrics:UIBarMetricsDefault];
    // 去除下边框
    [nav setShadowImage:[self createImageWithColor:[UIColor clearColor]]];
    // 设置标题的样式
    [nav setTitleTextAttributes:@{
        NSFontAttributeName: [UIFont systemFontOfSize:19],
        NSForegroundColorAttributeName: [UIColor whiteColor]
    }];
    // 设置左边按钮
    UIBarButtonItem *leftBtn= [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"home_btn_combination"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(openCombination)];
    
    navItem.leftBarButtonItem = leftBtn;
    
    [nav setItems:@[navItem] animated:YES];
    [self.view addSubview:nav];
}

/**
 加载首页数据,除了商品列表
 */
-(void) loadHome {
    HomeLoader *loader = [[HomeLoader alloc]init];
    //    __weak typeof(self) weakSelf = self;
    [loader loadDataWithFinishBlock:^(BOOL success, NSMutableDictionary * _Nonnull data) {
        //        __strong typeof(weakSelf) strongSelf = weakSelf;
        NSLog(@"活动 %@", data);
    }];
}

-(void) viewWillDisappear:(BOOL)animated {
    // 将状态栏信息的颜色改回默认的黑色
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

/**
 隐藏nav的下部边框线

 @param color 接受传来的颜色,透明不就消失了
 @return 返回根据颜色生成的图
 */
- (UIImage *)createImageWithColor:(UIColor *)color{
    
    CGRect rect = CGRectMake(0.0f,0.0f,1.0f,1.0f);
    
    UIGraphicsBeginImageContext(rect.size);
    
    CGContextRef context =UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    
    CGContextFillRect(context, rect);
    
    UIImage *theImage =UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return theImage;
    
}


/// 打开组合页面
-(void)openCombination {
    CombinationViewController *CombinationVC = [[CombinationViewController alloc]init];
    [self.navigationController pushViewController:CombinationVC animated:YES];
}

#pragma mark --实现delegate
// 实现导航栏通顶状态栏
- (UIBarPosition)positionForBar:(id <UIBarPositioning>)bar {
    return UIBarPositionTopAttached;
}
@end
